package com.qianfeng.wuyang.himalaya.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.DividerGridItemDecoration;
import com.qianfeng.wuyang.himalaya.activity.LocalActivity;
import com.qianfeng.wuyang.himalaya.activity.NationalActivity;
import com.qianfeng.wuyang.himalaya.activity.NetActivity;
import com.qianfeng.wuyang.himalaya.activity.ProvinceActivity;
import com.qianfeng.wuyang.himalaya.activity.RadiochartsActivity;
import com.qianfeng.wuyang.himalaya.adapter.broadcastadapter.BCGridRecyclerAdapter;
import com.qianfeng.wuyang.himalaya.adapter.broadcastadapter.BCLocalRecyclerAdapter;
import com.qianfeng.wuyang.himalaya.adapter.broadcastadapter.BCRadiocharsRecyclerAdapter;
import com.qianfeng.wuyang.himalaya.adapter.broadcastadapter.BCRankingRecyclerAdapter;
import com.qianfeng.wuyang.himalaya.bean.broadcastbean.BC_Entity;
import com.qianfeng.wuyang.himalaya.bean.broadcastbean.BC_topRadiosEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A simple {@link Fragment} subclass.
 */
public class BroadcastFragment extends Fragment implements Callback<BC_Entity>, View.OnClickListener {


    private BCGridRecyclerAdapter bcGridRecyclerAdapter;
    private RecyclerView localRecycler_bc;
    private BCLocalRecyclerAdapter bcLocalRecyclerAdapter;
    private RecyclerView rankingRecycler_bc;
    private BCRankingRecyclerAdapter bcRankingRecyclerAdapter;
    private TextView rankingmore;
//    private BCRadiocharsRecyclerAdapter bcRadiocharsRecyclerAdapter;
    private ArrayList<BC_topRadiosEntity>  radioChartsList;
    private LinearLayout rg_broadcast;

    public BroadcastFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_broadcast, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        com.qianfeng.wuyang.himalaya.activity.MyApplication myApplication = (com.qianfeng.wuyang.himalaya.activity.MyApplication) getActivity().getApplication();
//       MyApplication myApplication = (MyApplication) getActivity().getApplication();
        //拿到service对象执行异步下载
        myApplication.getBroadcastService().getData().enqueue(this);
        RecyclerView  gridRecycler = (RecyclerView) view.findViewById(R.id.gridRecycler_bc);
       //gridrecycler的操作
        bcGridRecyclerAdapter = new BCGridRecyclerAdapter(getContext());
        gridRecycler.setLayoutManager(new GridLayoutManager(getContext(),4));
        gridRecycler.setAdapter(bcGridRecyclerAdapter);
        gridRecycler.addItemDecoration(new DividerGridItemDecoration(getContext()));

        //localRecycler_bc的操作
        localRecycler_bc = ((RecyclerView) view.findViewById(R.id.localRecycler_bc));
        bcLocalRecyclerAdapter = new BCLocalRecyclerAdapter(getContext());
        localRecycler_bc.setLayoutManager(new LinearLayoutManager(getContext()));
        localRecycler_bc.setAdapter(bcLocalRecyclerAdapter);
        localRecycler_bc.setNestedScrollingEnabled(false);

        //排行榜的recyclerview
        rankingRecycler_bc = ((RecyclerView) view.findViewById(R.id.rankingRecycler_bc));
        rankingRecycler_bc.setLayoutManager(new LinearLayoutManager(getContext()));
        bcRankingRecyclerAdapter = new BCRankingRecyclerAdapter(getContext());
        rankingRecycler_bc.setAdapter(bcRankingRecyclerAdapter);
        rankingRecycler_bc.setNestedScrollingEnabled(false);

        //排行榜的更多按钮的代码
        rankingmore = ((TextView) view.findViewById(R.id.ranking_more));
        rankingmore.setOnClickListener(this);
       radioChartsList = new ArrayList<>();
        //radiogroup的操作
        rg_broadcast = ((LinearLayout) view.findViewById(R.id.rg_broadcast));
//        rg_broadcast.setOnCheckedChangeListener(this);
        for (int i = 0; i < rg_broadcast.getChildCount(); i++) {
            rg_broadcast.getChildAt(i).setOnClickListener(this);
        }
    }

    @Override
    public void onResponse(Call<BC_Entity> call, Response<BC_Entity> response) {
//        Log.i("body","wqeee"+response.body().getData().getCategories().get(0).getName());
       //表格的adapter的数据
        bcGridRecyclerAdapter.addAll(response.body().getData().getCategories());
//        Log.i("#######","234"+response.body().getData().getLocalRadios().get(0).getCoverSmall());
        //本地的adapter的数据
        bcLocalRecyclerAdapter.addAll(response.body().getData().getLocalRadios());
        //排行榜的adapter数据
        bcRankingRecyclerAdapter.addAll(response.body().getData().getTopRadios());
        //广播界面电台排行榜的数据
        radioChartsList.addAll(response.body().getData().getTopRadios());
    }

    @Override
    public void onFailure(Call<BC_Entity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
    //排行榜更多按钮的监听
    @Override
    public void onClick(View view) {

        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.rb_local:
                intent.setClass(getContext(), LocalActivity.class);
                break;
            case R.id.rb_country:
                intent.setClass(getContext(), NationalActivity.class);
                break;
            case R.id.rb_province:
                intent.setClass(getContext(), ProvinceActivity.class);
                break;
            case R.id.rb_network:
                intent.setClass(getContext(), NetActivity.class);
                break;
            case R.id.ranking_more:
                intent.setClass(getContext(), RadiochartsActivity.class);
                if (radioChartsList.size() != 0) {
                    intent.putParcelableArrayListExtra("list",radioChartsList);
                }
                break;
        }
        startActivity(intent);
    }

}
